package com.CGFrame.gantt;

import java.awt.Color;
import java.text.SimpleDateFormat;

import java.util.Date;


import javax.swing.JApplet;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.IntervalCategoryDataset;
import org.jfree.data.gantt.Task;
import org.jfree.data.gantt.TaskSeries;
import org.jfree.data.gantt.TaskSeriesCollection;


public class GanttApplet extends JApplet {
	TaskSeries s1 = new TaskSeries("Scheduled");
	TaskSeries s2 = new TaskSeries("Actual");
	StringBuffer ids = new StringBuffer();
	private String id;
	private String type;
	private String name;
	private Date startTime;
	private Date endTime;
	private String tips;
	private Double percent=0.0;
/*	
	public void init()
	{
		for (int i = 0 ; i<10;i++)
		{
		setId("1");
		setType("Element");
		setName("wolf"+i);
		setStartTime("2007-05-18 00:00:00.0");
		setEndTime("2007-05-22 00:00:00.0");
		setTips("hwu");
		addTask();
		
		}
		drawChart();
	}
*/	
	public void setId(String id)
	{
		this.id = id;
	}
	public void setType(String type)
	{
		this.type = type;
	}
	
	public void setName(String name)
	{
		this.name = name;
	}
	
	public void setStartTime(String startTime)
	{
		try
		{
			SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
			this.startTime  = df.parse(startTime);
		}
		catch(Exception e)
		{
		}
	}
	
	public void setEndTime(String endTime)
	{
		try
		{
			SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
			this.endTime  = df.parse(endTime);
		}
		catch(Exception e)
		{
		}
	}
	
	public void setTips(String tips)
	{
		this.tips = tips;
	}
	
	public void setPercent(String percent)
	{
		try
		{
		this.percent = Double.parseDouble(percent);
		}
		catch(Exception e)
		{
			this.percent=0.0;
		}
	}
	public void addScheduleTask()
	{
		Task task = new Task(name,startTime,endTime);
	//	task.setPercentComplete(percent);
		s1.add(task);
		ids.append(type+","+id+","+tips+";");
	}
	public void addActualTask()
	{
		Task task = new Task(name,startTime,endTime);
		task.setPercentComplete(percent);
		s2.add(task);
		ids.append(type+","+id+","+tips+";");
	}
	public void drawChart()
	{
		TaskSeriesCollection collection = new TaskSeriesCollection();
		collection.add(s1);
		collection.add(s2);
		IntervalCategoryDataset dataset = collection;
		JFreeChart chart = createChart(dataset);
		CategoryPlot plot = (CategoryPlot) chart.getPlot();
		plot.setNoDataMessage("NoData");
		CategoryItemRenderer renderer = plot.getRenderer();
		renderer.setSeriesPaint(0, Color.blue);
		renderer.setSeriesPaint(1, Color.red);
		renderer.setToolTipGenerator(new CustomGanttToolTips(ids) );
		final ChartPanel chartPanel = new ChartPanel(chart);
		setContentPane(chartPanel);
		validate();
		
	}
	
	
	
	JFreeChart createChart(final IntervalCategoryDataset dataset) {
        final JFreeChart chart = ChartFactory.createGanttChart(
            null,  // chart title
            "",              // domain axis label
            "Date",              // range axis label
            dataset,             // data
            true,                // include legend
            true,                // tooltips
            false                // urls
        );
        
        return chart;    
    }
}
